from model.admin_model import Admin
from model.blog_model import Blog
from model.class_model import Class
from model.mail_model import Mail
from model.message_model import Message


def add_model(info, model):
    """
        @function add:将接口处理好的对象转换成model实例
        @param info：Dict 或 List
        @param model：String 需要转换什么模型
        @return model实例 或 model实例的列表
    """
    if isinstance(info, dict):
        if model == "admin":
            return Admin(**info)
        elif model == "blog":
            return Blog(**info)
        elif model == "class":
            return Class(**info)
        elif model == "mail":
            return Mail(**info)
        elif model == "message":
            return Message(**info)
    elif isinstance(info, list):
        infos = []
        for item in info:
            if model == "admin":
                infos.append(Admin(**item))
            elif model == "blog":
                infos.append(Blog(**item))
            elif model == "class":
                infos.append(Class(**item))
            elif model == "mail":
                infos.append(Mail(**item))
            elif model == "message":
                infos.append(Message(**item))
        return infos


def return_model(Model, key_list):
    """
        @function return_model:将查询出来的数据转换成部分需要的数据
        @return 返回一个 dict 或 list
    """
    if isinstance(Model, dict):
        new_data = {}
        for key in key_list:
            new_data[key] = Model[key]
        return new_data
    elif isinstance(Model, list):
        model_data = []
        for item in Model:
            new_data = {}
            for key in key_list:
                new_data[key] = item[key]
            model_data.append(new_data)
        return model_data
